/*
从2025年1月1日起，我国将用15年时间，逐步将男职工的法定退休年龄从原60周岁延迟到63周岁，
将女职工的法定退休年龄从原50周岁、55周岁，分别延迟到55周岁、58周岁。
男职工和原法定退休年龄为五十五周岁的女职工，法定退休年龄每四个月延迟一个月，分别逐步延迟至六十三周岁和五十八周岁；
原法定退休年龄为五十周岁的女职工，法定退休年龄每二个月延迟一个月，逐步延迟至五十五周岁。
*/
const MALE_ORIGINAL_RETIREMENT_AGE: i32 = 60;
const MALE_FEATURE_RETIREMENT_AGE: i32 = 63;
const FEMALE_50_ORIGINAL_RETIREMENT_AGE: i32 = 50;
const FEMALE_50_FEATURE_RETIREMENT_AGE: i32 = 55;
const FEMALE_55_ORIGINAL_RETIREMENT_AGE: i32 = 55;
const FEMALE_55_FEATURE_RETIREMENT_AGE: i32 = 58;
const MONTH_PEER_DELAYED_ONE: i32 = 2;
const MONTH_PEER_DELAYED_TWO: i32 = 4;
//2058年12月退休，退休年龄58岁, 延迟退休36个月

pub fn retire_time(time: &str, tp: &str) -> String {
    // 解析出生年月
    let birth_vec = time.split('-').collect::<Vec<&str>>();
    if birth_vec.len() != 2 {
        panic!("error input");
    }
    let birth_year = birth_vec[0].parse::<i32>().expect("invalid year");
    let birth_month = birth_vec[1].parse::<i32>().expect("invalid month");

    // 解析人员类型并计算退休日期
    let (is_male, female_age) = if tp == "男职工" {
        (true, None)
    } else if tp == "原法定退休年龄50周岁女职工" {
        (false, Some(50))
    } else if tp == "原法定退休年龄55周岁女职工" {
        (false, Some(55))
    } else {
        panic!("error input");
    };
    // 计算男职工的退休年龄
    if is_male {
        if birth_year > 1977 {
            return format!(
                "{}-{:02},{:0.2},{}",
                MALE_FEATURE_RETIREMENT_AGE + birth_year,
                birth_month,
                MALE_FEATURE_RETIREMENT_AGE,
                36
            );
        } else if birth_year < 1965 {
            return format!(
                "{}-{:02},{:0.2},{}",
                MALE_ORIGINAL_RETIREMENT_AGE + birth_year,
                birth_month,
                MALE_ORIGINAL_RETIREMENT_AGE,
                0
            );
        } else {
            return calculate_retirement_age(
                birth_year,
                birth_month,
                MALE_ORIGINAL_RETIREMENT_AGE,
                MONTH_PEER_DELAYED_TWO,
            );
        }
    } else {
        let female_age = female_age.unwrap();
        // 计算女性职工的退休年龄
        if female_age == 55 {
            if birth_year < 1970 {
                return format!(
                    "{}-{:02},{:0.2},{}",
                    FEMALE_55_ORIGINAL_RETIREMENT_AGE + birth_year,
                    birth_month,
                    FEMALE_50_FEATURE_RETIREMENT_AGE,
                    0
                );
            } else if birth_year > 1985 {
                return format!(
                    "{}-{:02},{:0.2},{}",
                    FEMALE_55_FEATURE_RETIREMENT_AGE + birth_year,
                    birth_month,
                    FEMALE_55_FEATURE_RETIREMENT_AGE,
                    36
                );
            } else {
                return calculate_retirement_age(
                    birth_year,
                    birth_month,
                    FEMALE_55_ORIGINAL_RETIREMENT_AGE,
                    MONTH_PEER_DELAYED_TWO,
                );
            }
        } else {
            if birth_year < 1975 {
                return format!(
                    "{}-{:02},{:0.2},{}",
                    FEMALE_50_ORIGINAL_RETIREMENT_AGE + birth_year,
                    birth_month,
                    FEMALE_50_ORIGINAL_RETIREMENT_AGE,
                    0
                );
            } else if birth_year > 1985 {
                return format!(
                    "{}-{:02},{:0.2},{}",
                    FEMALE_50_FEATURE_RETIREMENT_AGE + birth_year,
                    birth_month,
                    FEMALE_50_FEATURE_RETIREMENT_AGE,
                    60
                );
            } else {
                return calculate_retirement_age(
                    birth_year,
                    birth_month,
                    FEMALE_50_ORIGINAL_RETIREMENT_AGE,
                    MONTH_PEER_DELAYED_ONE,
                );
            }
        }
    }
}
fn calculate_retirement_age(
    birth_year: i32,
    birth_month: i32,
    original_retirement_age: i32,
    month_peer: i32,
) -> String {
    // 计算原退休时间年
    let original_retirement_year = birth_year + original_retirement_age;

    // 计算从出生年到原退休年之间的月份数
    let m = (original_retirement_year - 2025) * 12 + birth_month;

    // 计算延迟的总月份
    let mut delayed_months = m / month_peer;

    // 如果延迟的月数不足一个周期 (例如不到4个月), 则按1个月延迟
    if m % month_peer != 0 {
        delayed_months += 1;
    }

    // 计算延迟的年和月
    let mut delayed_years = delayed_months / 12;
    let mut future_month = delayed_months % 12;

    // 处理月份和年份的调整
    if future_month + birth_month > 12 {
        delayed_years += 1;
        future_month = future_month + birth_month - 12;
    } else {
        future_month = future_month + birth_month;
    }

    // 计算延迟后的年份
    let future_year = original_retirement_year + delayed_years;

    // 计算实际退休年龄（包括延迟年数）
    let retirement_age = original_retirement_age as f32 + (delayed_months as f32 / 12.0);

    // 返回未来的退休年月，延迟年数，和延迟月数
    format!(
        "{}-{:02},{:0.2},{}",
        future_year, future_month, retirement_age, delayed_months
    )
}
